package com.kfd.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kfd.demo.entity.Reqs;
import com.kfd.demo.service.CouponServe;
import com.kfd.demo.util.JsonConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin(origins = "*")
public class CouponController {
//    优惠券

    @Autowired
    CouponServe couponServe;


//    获取用户所有优惠券
    @ResponseBody
    @PostMapping(value = "/getmyCoupons", produces = "application/json;charset=UTF-8")
    public Reqs getCouponController(Integer  uid) throws JsonProcessingException {
//        System.out.println(uid+"我是uid");
        List mycoups = couponServe.geUsertCoupons(uid);

//        Map<String, Object> myCoupons = new HashMap<>();
//        myCoupons.put("data", mycoups);
//        myCoupons.put("meta", 200);
//        return JsonConvertUtil.toJsonString(mycou ps);
        return new Reqs(200,"", mycoups);
    }


    //获取优惠券
    @ResponseBody
    @PutMapping(value = "/RedCode", produces = "application/json;charset=UTF-8")
    public Reqs RedCode(String redcode,int uid) {
//        couponServe.RedCode(redcode,uid);
        return couponServe.RedCode(redcode,uid);
    }

    //使用优惠券
    @ResponseBody
    @DeleteMapping(value = "/buyCoupon", produces = "application/json;charset=UTF-8")
    public Reqs ByCoupon(int coupid,int uid) {
        return new Reqs(200,"使用优惠券",couponServe.delCoupon(uid,coupid));

    }


}
